package com.jiangxiaofei.citycloud.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jiangxiaofei.citycloud.R;
import com.jiangxiaofei.citycloud.adapter.HomeFragmentPagerAdapter;
import com.jiangxiaofei.citycloud.base.BaseActivity;
import com.jiangxiaofei.citycloud.event.NetMessage;
import com.jiangxiaofei.citycloud.fragment.CityCountryFrament;
import com.jiangxiaofei.citycloud.fragment.CompassFragment;
import com.jiangxiaofei.citycloud.fragment.MainFragment;
import com.jiangxiaofei.citycloud.fragment.MeFragment;
import com.jiangxiaofei.citycloud.util.NetUtils;
import com.jiangxiaofei.citycloud.view.NonSlipViewPager;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * @author JiangXFei
 *         主页面
 */

public class HomeActivity extends BaseActivity implements ViewPager.OnPageChangeListener {

    @InjectView(R.id.home_vp)
    NonSlipViewPager homeVp;
    @InjectView(R.id.home_tv_menu_home)
    TextView homeTvMenuHome;
    @InjectView(R.id.home_tv_menu_city)
    TextView homeTvMenuCity;
    @InjectView(R.id.home_img_menu_compass)
    TextView homeImgMenuCompass;
    @InjectView(R.id.home_tv_menu_me)
    TextView homeTvMenuMe;
    @InjectView(R.id.linear_menu)
    LinearLayout linearMenu;
    private List<Fragment> fragments = new ArrayList<>();
    private NetChangeBrocastReceiver netChangeBrocastReceiver;

    @Override
    protected void initView(Bundle savedInstanceState) {
        mActivity = this;
        mContext = this;
        setContentView(R.layout.activity_home_layout);
        ButterKnife.inject(this);
        initFragment();
        IntentFilter filter = new IntentFilter();
        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        netChangeBrocastReceiver = new NetChangeBrocastReceiver();
        this.registerReceiver(netChangeBrocastReceiver, filter);

    }



    /**
     * 初始化fragment
     */
    private void initFragment() {
        MainFragment mainFragment = new MainFragment();
        CityCountryFrament cityFrament = new CityCountryFrament();
        MeFragment meFragment = new MeFragment();
        CompassFragment serviceFragment = new CompassFragment();
//        fragments.add(mainFragment);
        fragments.add(cityFrament);
        fragments.add(serviceFragment);
//        fragments.add(meFragment);
        FragmentManager fm = getSupportFragmentManager();
        HomeFragmentPagerAdapter homeFragmentPagerAdapter = new HomeFragmentPagerAdapter(fm, fragments);
        homeVp.setAdapter(homeFragmentPagerAdapter);
        homeVp.setCurrentItem(0);
        homeVp.addOnPageChangeListener(this);
    }


    @Override
    protected void setListener() {

    }

    @Override
    protected void processLogic(Bundle savedInstanceState) {

    }


    @OnClick({R.id.home_tv_menu_home,
            R.id.home_tv_menu_city,
            R.id.home_tv_menu_me,
            R.id.home_img_menu_compass})
    public void OnClick(View v) {
        switch (v.getId()) {
//            case R.id.home_tv_menu_home:
//                //首页
//                initMenuColor();
//                homeVp.setCurrentItem(0);
//                homeTvMenuHome.setTextColor(getResources().getColor(R.color.HomeTextColorSelect));
//                homeTvMenuHome.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.ic_home_sel), null, null);
//                break;
            case R.id.home_tv_menu_city:
                //城市
                initMenuColor();
                homeVp.setCurrentItem(0);
                homeTvMenuCity.setTextColor(getResources().getColor(R.color.HomeTextColorSelect));
                homeTvMenuCity.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.ic_city_country_sel), null, null);
                break;
//            case R.id.home_tv_menu_me:
//                //我的
//                initMenuColor();
//                homeVp.setCurrentItem(3);
//                homeTvMenuMe.setTextColor(getResources().getColor(R.color.HomeTextColorSelect));
//                homeTvMenuMe.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.ic_me_sel), null, null);
//                break;
            case R.id.home_img_menu_compass:
                //城市罗盘
                initMenuColor();
                homeVp.setCurrentItem(1);
                homeImgMenuCompass.setTextColor(getResources().getColor(R.color.HomeTextColorSelect));
                homeImgMenuCompass.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.ic_parser_sel), null, null);
                break;
            default:
                break;
        }
    }

    //初始化菜单栏
    private void initMenuColor() {
        homeTvMenuHome.setTextColor(getResources().getColor(R.color.HometextColor));
        homeTvMenuCity.setTextColor(getResources().getColor(R.color.HometextColor));
        homeTvMenuMe.setTextColor(getResources().getColor(R.color.HometextColor));
        homeImgMenuCompass.setTextColor(getResources().getColor(R.color.HometextColor));
        homeTvMenuHome.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.ic_home), null, null);
        homeTvMenuCity.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.ic_city_country), null, null);
        homeTvMenuMe.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.ic_me), null, null);
        homeImgMenuCompass.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.ic_parser), null, null);
    }


    /**
     * viewPager改变监听
     *
     * @param position
     * @param positionOffset
     * @param positionOffsetPixels
     */
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        initMenuColor();
        switch (position) {
//            case 0:
//                homeTvMenuHome.setTextColor(getResources().getColor(R.color.HomeTextColorSelect));
//                homeTvMenuHome.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.ic_home_sel), null, null);
//                break;
//            case 1:
//                homeTvMenuCity.setTextColor(getResources().getColor(R.color.HomeTextColorSelect));
//                homeTvMenuCity.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.ic_city_country_sel), null, null);
//                break;
//            case 2:
//                homeImgMenuCompass.setTextColor(getResources().getColor(R.color.HomeTextColorSelect));
//                homeImgMenuCompass.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.ic_parser_sel), null, null);
//                break;
//            case 3:
//                homeTvMenuMe.setTextColor(getResources().getColor(R.color.HomeTextColorSelect));
//                homeTvMenuMe.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.ic_me_sel), null, null);
//                break;
            case 0:
                homeTvMenuCity.setTextColor(getResources().getColor(R.color.HomeTextColorSelect));
                homeTvMenuCity.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.ic_city_country_sel), null, null);
                break;
            case 1:
                homeImgMenuCompass.setTextColor(getResources().getColor(R.color.HomeTextColorSelect));
                homeImgMenuCompass.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.ic_parser_sel), null, null);
                break;

            default:
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        this.unregisterReceiver(netChangeBrocastReceiver);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    class NetChangeBrocastReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (NetUtils.isConnected(mContext)) {
                EventBus.getDefault().post(new NetMessage(true));
            } else {
                startActivity(new Intent(mContext, NetBadActivity.class));
            }
        }
    }

}
